import 'dart:developer';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/my/team_focus_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/api/user_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyFocusTeamPage extends StatefulWidget {
  const MyFocusTeamPage({super.key});

  @override
  State<MyFocusTeamPage> createState() => _MyFocusTeamPageState();
}

class _MyFocusTeamPageState extends State<MyFocusTeamPage> {
  final refreshController = EasyRefreshController();
  final scrollC = ApmScrollController();

  getFocusTeams() async {
    teams = await UserApi.getTeamFocusList();
    update();
  }

  focusTeam(TeamFocusEntity team) async {
    if ((await UserApi.focusTeam(team.id, 1)) == 200) {
      team.focus = 1;
      update();
    } else {
      Toast.text("关注失败");
    }
  }

  unfocusTeam(TeamFocusEntity team) async {
    if ((await UserApi.unFocusTeam(team.id, 1)) == 200) {
      team.focus = 0;
      update();
    } else {
      Toast.text("取消关注失败");
    }
  }

  refresh() {
    refreshController.callRefresh(scrollController: scrollC);
  }

  List<TeamFocusEntity>? teams;

  @override
  void initState() {
    getFocusTeams();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colours.scaffoldBg,
      child: Column(
        children: [
          Expanded(
            child: EasyRefresh(
              controller: refreshController,
              onRefresh: () async {
                await getFocusTeams();
              },
              child: teams == null
                  ? Container()
                  : (teams!.isEmpty
                      ? const NoDataWidget(
                          tip: '暂无关注的球队',
                          needScroll: true,
                        )
                      : ListView(
                          controller: scrollC,
                          children: [
                            ...teams!.map(
                              (e) => cell(e).marginOnly(top: 8),
                            )
                          ],
                        )),
            ),
          ),
          Container(
            color: Colours.white,
            child: SafeArea(
                child: toFocusBtn().tap(() {
              Utils.onEvent('wd_wdgz_gzan', params: {'wd_wdgz_gzan': '3'});
              Get.toNamed(Routes.myTofocusTeam)?.then((value) => refresh());
            })),
          )
        ],
      ),
    );
  }

  Widget toFocusBtn() => Container(
        height: 48,
        width: double.infinity,
        alignment: Alignment.center,
        margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
        decoration: BoxDecoration(
            color: Colours.main, borderRadius: BorderRadius.circular(8)),
        child: QsText("关注更多球队",
            color: Colours.white, fontSize: 16, fontWeight: FontWeight.w500),
      );

  Widget cell(TeamFocusEntity entity) => Container(
        height: 68,
        margin: const EdgeInsets.symmetric(horizontal: 8),
        padding: const EdgeInsets.all(12),
        decoration: BoxDecoration(
            color: Colours.white, borderRadius: BorderRadius.circular(8)),
        child: Row(
          children: [
            Row(
              children: [
                Container(
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(4)),
                  width: 44,
                  height: 44,
                  clipBehavior: Clip.hardEdge,
                  child: CachedNetworkImage(
                      imageUrl: entity.logo ?? "",
                      errorWidget: (context, url, error) =>
                          Styles.placeholderIcon(),
                      placeholder: (context, url) => Styles.placeholderIcon()),
                ),
                const SizedBox(
                  width: 8,
                ),
                QsText(
                  entity.nameChsShort ?? "",
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                ),
              ],
            ).tap(() {
              Get.toNamed(Routes.zqTeamDetail, arguments: entity.id);
            }),
            const Spacer(),
            const SizedBox(
              width: 8,
            ),
            Container(
              height: 24,
              width: 56,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(
                      width: 0.5,
                      color: entity.isFocus ? Colours.grey99 : Colours.main)),
              child: QsText(
                entity.isFocus ? "已关注" : "关注",
                color: entity.isFocus ? Colours.grey99 : Colours.main,
                fontSize: 13,
              ),
            ).tap(() {
              log("tap aaa");
              if (entity.isFocus) {
                unfocusTeam(entity);
              } else {
                focusTeam(entity);
              }
            })
          ],
        ),
      );
}
